#ifdef GL_ES
precision mediump float;
// 定义 PRECISION 为空，保持向后兼容
#define PRECISION
#else
#define PRECISION
#endif

// 纹理采样器
uniform sampler2D u_texture;
uniform float u_time;

// 从顶点着色器传递来的变量
varying float v_time;
varying vec4 v_position;
varying vec4 v_color;
varying vec2 v_texCoords;
varying float v_plantBottomY;
varying vec2 v_uvSize;
varying float v_uvV2;

varying float v_base_height;
varying float v_swingIntensity;

void main() {
    //    //测试每图块数据变化
    //    vec4 color = texture2D(u_texture, v_texCoords);
    //    color.rgb = vec3(v_uvSize.x * 1.0, 0.0, 1.0);
    //    gl_FragColor = color;

    // 摆动效果
    float intensity = v_swingIntensity;
    float frequency = 2.5;

    float uvLocalY = v_texCoords.y - v_uvV2;
    float uvNormalY = uvLocalY / v_uvSize.y + v_base_height;
    float fragIntensity = uvNormalY * intensity;
    float factorX = v_uvSize.x / 1.0;
    float factorY = v_uvSize.y / 1.0;
    float swingX = sin(v_texCoords.y * 15.0 / (4.0 * v_uvSize.y) + v_time * frequency) * 0.01 * factorX * 15.0;
    //    float swingY = -sin(v_texCoords.x * 15.0 / (4.0*v_uvSize.x) + v_time * frequency) * 0.01 * factorY*15.0;
    vec2 distortion = vec2(swingX, 0);
    distortion *= fragIntensity;
    vec2 modifiedCoords = v_texCoords + distortion;

    if (modifiedCoords.x > 1.0 || modifiedCoords.x < 0.0 || modifiedCoords.y > 1.0 || modifiedCoords.y < 0.0) gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
    else gl_FragColor = texture2D(u_texture, modifiedCoords);
}
